#!/system/bin/sh
# 用户级保活
# APK名称
ApkName=/data/local/tmp/cloud-phone.apk
# APK包名
PackageName=com.cloud.phone
# Service
ServiceName=com.cloud.phone/.service.CloudService
#卸载apk
UNINSTALL_APK="pm uninstall $PackageName"

InstallService() {
     count=`pm list packages | grep $PackageName |wc -l`
           if [ $count -le 0 ]; then
                   echo "$ApkName is not installed, now install."
                   ret=`pm install $ApkName`
                   Succ="Success"
                   if [ "$ret" == "$Succ" ]; then
                           echo "install succeed."
                   else
                           echo "install failed."
                   fi
           else
                   echo "$ApkName is already installed."
                   echo "Service start by boot_complete broadcast."
           fi
           return 0

}

StartService() {
    ret=`am startservice $ServiceName`
    Err="Error: Not found; no service started."
        contain=$(echo $ret | grep "${Err}")
        if [[ "$contain" != "" ]]; then
                echo "Service start failed."
                #exit 1
        else
                echo "Service start succeed."
        fi
        return 0
}

# 设置应用包名
APP_PACKAGE=com.cloud.phone

# 定义查询应用是否运行的命令
AM_PID_CMD="ps -ef|grep $APP_PACKAGE|grep -v grep"

# 定义拉起应用的命令
LAUNCH_APP_CMD="am start $APP_PACKAGE/.MainActivity"

# 间隔时间（秒）
SLEEP_TIME=10

StartActivity(){
    if [ -z "$(${AM_PID_CMD})" ]; then
       echo "应用没有运行，尝试拉起..."
        ${LAUNCH_APP_CMD}
    else
       echo "应用正在运行..."
       #若后台服务正在运行exit 1 退出 continue继续.
       #exit 0 正常退出
     fi
}

# 设置后台进程名称
SERVICE_NAME=com.cloud.phone/.service.CloudService

# 定义查询后台进程是否运行的命令
AM_SERVICE_CMD="ps -ef|grep $SERVICE_NAME|grep -v grep"

# 定义拉起后台进程的命令
LAUNCH_SERVICE_CMD="am start-foreground-service $SERVICE_NAME"

# 间隔时间（秒）
SLEEP_TIME=20

CheckForegroundService(){
    while true; do
      sleep ${SLEEP_TIME}
       ${LAUNCH_UPDATE_APP_BROADCAST}
       if [ -z "$(${AM_SERVICE_CMD})" ]; then
        echo "后台服务没有运行，尝试拉起..."
         ${LAUNCH_SERVICE_CMD}
       else
        echo "后台服务正在运行..."
        #若后台服务正在运行exit 1 退出 continue继续.
        #exit 0 正常退出
        continue
      fi
    done
}

# 设置应用包名
APP_PACKAGE=com.cloud.phone

# 定义app后台被杀死白名单
ADD_BG_KILLER_WHITE_APP="dumpsys cph_performance addBgKillerWhiteApp $APP_PACKAGE"

# 定义app自动拉起白名单
ADD_AUTO_RUN_WHITE_APP="dumpsys cph_performance addAutoRunWhiteApp $APP_PACKAGE"

AddWhiteKillApp(){
  ${ADD_BG_KILLER_WHITE_APP}
  ${ADD_AUTO_RUN_WHITE_APP}
}

# ime服务名称
IME_SERVICE_NAME=com.huawei.cloudphoneime/.CaeImeService

# 定义查询后台ime服务进程是否运行的命令
AM_IME_SERVICE_CMD="ps -ef|grep $IME_SERVICE_NAME|grep -v grep"

# 定义拉起后台ime服务进程的命令
LAUNCH_IME_SERVICE_CMD="am start-foreground-service $IME_SERVICE_NAME"

openHwIme(){
    ${LAUNCH_IME_SERVICE_CMD}
}

# 设置应用包名
APP_PACKAGE=com.cloud.phone

AUTO_BG_KILLER_CMD="cmd performance bg-killer -a $APP_PACKAGE"

AUTO_RUN_CMD="cmd performance auto-run -a  $APP_PACKAGE"

AutoAccessibility(){
  ${AUTO_BG_KILLER_CMD}
  ${AUTO_RUN_CMD}
}

RECEIVER_NAME=com.cloud.phone/.broadcastReceiver.AppUpdateReceiver
# 定义应用被杀死广播的参数
PACKAGE_ACTION="android.intent.action.PACKAGE_ADDED"
# 使用adb发送广播
LAUNCH_UPDATE_APP_BROADCAST="am broadcast -a $PACKAGE_ACTION $RECEIVER_NAME"

StartAppInstallBroadcast(){

     # 检查是否成功发送
     if [ $? -eq 0 ]; then
        echo "应用更新广播成功发送"
    else
        echo "应用更新广播发送失败"
    fi
}

main() {
        # 安装应用
        InstallService
        echo "To start when first installed."

         # 开启无障碍服务权限
        AutoAccessibility
        echo "open auto accessibility"

        # 启动服务
        StartService
        echo "To start service for cloudService."

        # 拉起应用
        StartActivity
        echo "start activity for cloudPhone."

        # 开启Ime
        openHwIme
        echo "open ime service"

        #将应用加入白名单
        AddWhiteKillApp

        # 应用安装广播
        #StartAppInstallBroadcast

        # 检查后台进程是否存在
        CheckForegroundService
        echo "check service is running."
}

main
